﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data.SqlClient;
using models;

namespace DAL
{
   public class CartService
    {
        //购物车
        //查询当前用户的购物车
        //新增商品到购物车
        public bool AddCart(Cart cart) 
        {
            string sql = string.Format("insert cart values('{0}',{1},{2},{3})", cart.Count, cart.Userid, cart.Storeid, cart.Proid);
            return DBhelper.ex(sql);
        }
        public SqlDataReader SelCart(string Userid) 
        {                   //int    int  int   string  string  stting s    int     s     int     s                              
            //购物车内显示出购物车ID 数量 商品id 商品名 商品图片 规格 单价 店家id 店家 买家id 买家账号
            string sql = string.Format("select  [count]* p.Price uSum ,c.Id,c.[Count],p.Id,p.ProName,p.Pic,Speci,Price ,s.Id,s .storeName,u.Id,u.Uid  from cart c join store s on c.StoreID = s.Id join[User] u on c.UserId = u.Id join product p on c.ProID = p.Id where u.Id='{0}'", Userid);
            return DBhelper.DataReader(sql);
        }
        //删除购物车中的商品
        public bool DelCart(string CartId) 
        {
            String sql = string.Format("delete Cart where id={0}", CartId);
            return DBhelper.ex(sql);
        }
        //清空购物车中的商品
        public bool DelCartSum(string UserId)
        {
            String sql = string.Format("delete Cart where Userid={0}", UserId);
            return DBhelper.ex(sql);
        }

        //增加或减少购物车选择物品的数量
        public bool AddSubCartCount(string id,string cz) 
        {
                
            string sql = string.Format("update cart set [count]{1}=1 where id='{0}'",id,cz);
            return DBhelper.ex(sql);
        }
        //当有人直接改变TexT的值时直接将Count变了
        public bool UPtextCartCount(string id,string Sum) 
        {
            string sql = string.Format("update Cart ser [count]='{1}' where id='{0}'", id, Sum);
            return DBhelper.ex(sql);
        }
        //单物品总价
        public SqlDataReader SumCartById(string id)
        {
            string sql = string.Format("select  [count]* p.Price uSum  from cart c join store s on c.StoreID = s.Id join[User] u on c.UserId = u.Id join product p on c.ProID = p.Id where u.Id='{0}'",id);
            return DBhelper.DataReader(sql);
        }
        //全部总价显示
        public SqlDataReader SumCart(string id) 
        {
            string sql = string.Format("select sum ([count]* p.Price) SSum from cart c join store s on c.StoreID = s.Id join[User] u on c.UserId = u.Id join product p on c.ProID = p.Id where u.Id='{0}'",id);
            return DBhelper.DataReader(sql);
        }
        //新增物品到购物车
        public bool InsertCart(Product product, string Userid, int Count)
        {
            string sql = string.Format("insert Cart values('{0}','{1}','{2}','{3}')", Count, Userid, product.Stroe, product.Id);
            return DBhelper.ex(sql);
        }
        public SqlDataReader CartCount(string UserID) 
        {
            string sql= "select count(*) from cart where UserId="+ UserID ;
            return DBhelper.DataReader(sql);
            
        }
    }
}
